﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pahan.McuFusion.UI.Interfaces;

namespace Pahan.McuFusion.UI.Views
{
    public partial class WizardView : Form, IWizardView
    {
        public WizardView()
        {
            InitializeComponent();
        }

        #region Implementation of IWizardView

        /// <summary>
        /// Gets or sets the title of the wizard.
        /// </summary>
        public string Title
        {
            get { return this.Text; }
            set { this.Text = value; }
        }

        /// <summary>
        /// Gets or sets whether the user can navigate to the next page.
        /// </summary>
        public bool CanGoNext
        {
            get { return canGoNext && !canFinish; }
            set
            {
                this.canGoNext = value;
                if (!canFinish)
                {
                    btnNext.Enabled = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets whether the user can navigate to the previous page.
        /// </summary>
        public bool CanGoBack
        {
            get { return btnBack.Enabled; }
            set { btnBack.Enabled = value; }
        }

        /// <summary>
        /// Gets or sets whether the user can finish the dialog.
        /// </summary>
        public bool CanFinish
        {
            get { return canFinish; }
            set
            {
                canFinish = value;
                if (value)
                {
                    btnNext.Text = "&Finish";
                    btnNext.Enabled = true;
                }
                else
                {
                    btnNext.Text = "&Next >";
                    btnNext.Enabled = canGoNext;
                }
            }
        }

        /// <summary>
        /// Gets or sets whether the user can cancel the dialog.
        /// </summary>
        public bool CanCancel
        {
            get { return btnCancel.Enabled; }
            set { btnCancel.Enabled = value; }
        }

        /// <summary>
        /// Gets or sets the current page.
        /// </summary>
        public IWizardPage CurrentPage
        {
            get
            {
                return currentPage;
            }
            set
            {
                Control page = value as Control;
                if (page == null)
                {
                    throw value == null ?
                        new ArgumentNullException("value") :
                        new ArgumentException("The page must be a control.", "value");
                }
                lTitle.Text = value.Title;
                page.Dock = DockStyle.Fill;
                pPageContainer.Controls.Clear();
                pPageContainer.Controls.Add(page);
                currentPage = value;
            }
        }

        /// <summary>
        /// Show the wizard.
        /// </summary>
        /// <returns>
        /// True if the user has finished the wizard; 
        /// false if the user has cancelled the wizard.
        /// </returns>
        bool IWizardView.Show()
        {
            return this.ShowDialog() != DialogResult.Cancel;
        }

        /// <summary>
        /// Closes the wizard.
        /// </summary>
        public void Close(bool result)
        {
            this.DialogResult = result ?
                DialogResult.OK : 
                DialogResult.Cancel;
        }

        public event EventHandler NextClicked;
        
        public event EventHandler BackClicked;
        
        public event EventHandler FinishClicked;

        #endregion

        #region Private Members

        private IWizardPage currentPage;

        private bool canGoNext;

        private bool canFinish;

        #endregion

        private void OnBackClicked(object sender, EventArgs e)
        {
            var handler = BackClicked;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        private void OnNextClicked(object sender, EventArgs e)
        {
            var handler = canFinish ? FinishClicked : NextClicked;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
}
